<?php
declare (strict_types=1);

namespace app\api\controller;

use app\api\model\Member as Membermodel;
use app\BaseController;
use think\captcha\facade\Captcha;

class Login extends BaseController
{
    public function index()
    {
        $this->getSession();
        if ($this->request->isPost()) {
            //获取用户post过来的数据
            $data['user'] = $this->request->param('user');
            $data['password'] = $this->request->param('password');
            $data['captcha'] = $this->request->param('captcha');
            //验证post数据
            $Membermodel = new Membermodel();
            return $Membermodel->login($data);
        }

        return view();
    }

    protected function getSession()
    {
        if (session('admin.id')) {
            return $this->redirect((string)Url('api/index/index'));

        }
    }

    public function register()
    {

        if ($this->request->isPost()) {
            $data['user'] = $this->request->param('user');
            $data['password'] = $this->request->param('password');
            $data['passworda'] = $this->request->param('passworda');
            $data['captcha'] = $this->request->param('captcha');
            //验证post数据
            $Membermodel = new Membermodel();
            return $Membermodel->register($data);
        }
        $this->getSession();
        return view();
    }

    public function logout()
    {

        session(null);

        return ['code' => 1, 'msg' => '退出成功', 'url' => (string)Url('api/login/index')];

    }

    /**
     * 注册验证码
     * @Author: 闻子 < QQ：270988107 >
     */
    public function verify()
    {
        return Captcha::create('login');
    }




}